import {
  getMyProfitPerson,
  getMyProfitGift,
  getMyProfitGame,
  getMyProfitInviter,
} from "../../api/income";
import { handleWXRegister } from "../../api/user";
import { getRandom } from "../../utils/index";
// pages/income/income.js
import { SHOW_COUNT } from "../../config";

let showCount = {
  fans: SHOW_COUNT,
  gifts: SHOW_COUNT,
  invites: SHOW_COUNT,
  games: SHOW_COUNT,
};
let currentPage = {
  fans: 1,
  gifts: 1,
  invites: 1,
  games: 1,
};

let totalPage = {
  fans: 1,
  gifts: 1,
  invites: 1,
  games: 1,
};
let loading = {
  fans: false,
  gifts: false,
  invites: false,
  games: false,
};
const _tabNumFan = "tabNum.fans";

const _tabNumGift = "tabNum.gifts";

const _tabNumInvite = "tabNum.invites";

const _tabNumGame = "tabNum.games";

Page({
  data: {
    cardCur: 0,
    chooseTime: "2020-10-10",
    currentTabIndex: 0,
    fans_list: [],
    gifts_list: [],
    invites_list: [],
    moneyInfo: null,
    tabNum: {
      fans: "0",
      gifts: "0",
      invites: "0",
      games: "0",
    },
    endTime: "",
    user_money: getApp().globalData.user_money,
  },

  // cardSwiper
  cardSwiper(e) {
    ;
    this.setData({ currentTabIndex: e.detail.current });
    this.initOnloadData();
    switch (this.data.currentTabIndex) {
      case 0:
        this.initMyProfitGift();
        break;
      case 1:
        ;
        this.initMyProfitPerson();
        break;
      case 2:
        this.initMyProfitGame();
        break;
      case 3:
        this.initMyProfitInviter();
        break;
      default:
        break;
    }
  },
  timeChange(e) {
     
    this.setData({ chooseTime: e.detail.value });
    this.initOnloadData();
    switch (this.data.currentTabIndex) {
      case 0:
        this.initMyProfitGift();
        break;
      case 1:
        ;
        this.initMyProfitPerson();
        break;
      case 2:
        this.initMyProfitGame();
        break;
      case 3:
        this.initMyProfitInviter();
        break;
      default:
        break;
    }
  },

  tableTabClick(e) {
     
    let { index } = e.currentTarget.dataset;
    if (index != this.data.currentTabIndex) {
      this.setData({ currentTabIndex: parseInt(index) });
    } else {
      return;
    }
  },

  //init
  initOnloadData() {
    let user_money = getApp().globalData.user_money;
    // let total_person =  getApp().globalData.total_person;
    currentPage = {
      fans: 1,
      gifts: 1,
      invites: 1,
      games: 1,
    };

    totalPage = {
      fans: 1,
      gifts: 1,
      invites: 1,
      games: 1,
    };
    loading = {
      fans: false,
      gifts: false,
      invites: false,
      games: false,
    };

    this.setData({
      moneyInfo: null,
      invites_list: [],
      [_tabNumInvite]: 0,

      [_tabNumGame]: 0,

      gifts_list: [],
      [_tabNumGift]: 0,

      fans_list: [],
      [_tabNumFan]: 0,
      user_money: user_money,
      // total_person: total_person,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const today = new Date('2020-11-21');
    const today = new Date();
    const yesterday_time = today.getTime() - 1000 * 60 * 60 * 24; // 取昨天的日期

    const yesterday = new Date(yesterday_time);

    const year = yesterday.getFullYear();
    const month = yesterday.getMonth() + 1;
    const day = yesterday.getDate();

    this.setData({
      chooseTime: `${year}-${month > 9 ? month : "0" + month}-${
        day > 9 ? day : "0" + day
      }`,
      endTime: yesterday,
    });
    if (!getApp().globalData.token) {
      return;
    }
    this.initOnloadData();
    switch (this.data.currentTabIndex) {
      case 0:
        this.initMyProfitGift();
        break;
      case 1:
        ;
        this.initMyProfitPerson();
        break;
      case 2:
        this.initMyProfitGame();
        break;
      case 3:
        this.initMyProfitInviter();
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!getApp().globalData.token) {
      getApp().initUserInfo();
      return;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  withDrawHistory() {
    // wx.showToast({
    //   title: '暂无提现记录', icon: 'none'
    // })
    wx.navigateTo({
      url: "/pages/income/withDraw/history/history",
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page;
    switch (this.data.currentTabIndex) {
      case 0:
        if (currentPage.gifts > totalPage.gifts) {
          wx.showToast({
            title: "没有更多了",
            icon: "none",
          });
        } else {
          this.initMyProfitGift();
        }
        break;
      case 1:
        if (currentPage.fans > totalPage.fans) {
          wx.showToast({
            title: "没有更多了",
            icon: "none",
          });
        } else {
          this.initMyProfitPerson();
        }
        break;
      case 3:
        if (currentPage.invites > totalPage.invites) {
          wx.showToast({
            title: "没有更多了",
            icon: "none",
          });
        } else {
          this.initMyProfitInviter();
        }
        break;
      default:
        break;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  //methods

  // 新增粉丝
  initMyProfitPerson() {
    if (loading.fans) {
      return;
    }
    wx.showLoading({
      title: "正在加载中",
      icon: "none",
    });

    loading.fans = true;
    getMyProfitPerson({
      showCount: showCount.fans,
      currentPage: currentPage.fans,
      chooseTime: this.data.chooseTime,
    })
      .then((res) => {
        this.handleMoneyInfo(res.moneyInfo);
        if (!res.list) {
          this.setData({
            fans_list: [],
          });
        } else {
          for (let i = 0; i < res.list.length; i++) {
            res.list[i].sex = Math.random() > 0.5;
            res.list[i].level = getRandom(1, 4);
          }
          const list = this.data.fans_list;
          ;
          ;
          currentPage.fans++;
          totalPage.fans = res.totalPage;
          this.setData({
            fans_list: list.concat(res.list),
          });
        }
        loading.fans = false;
        wx.hideLoading();
      })
      .catch((err) => {
          
        loading.fans = false;
        wx.hideLoading();
      })
      // .finally(() => {
      //   loading.fans = false;
      //   wx.hideLoading();
      // });
  },

  // 新增礼物收益
  initMyProfitGift() {
    if (loading.gifts) {
      return;
    }
    wx.showLoading({
      title: "正在加载中",
      icon: "none",
    });
    loading.gifts = true;
    getMyProfitGift({
      showCount: showCount.gifts,
      currentPage: currentPage.gifts,
      chooseTime: this.data.chooseTime,
    })
      .then((res) => {
        this.handleMoneyInfo(res.moneyInfo);
        if (!res.list) {
          this.setData({
            gifts_list: [],
          });
        } else {
          const list = this.data.gifts_list;

          currentPage.gifts++;
          totalPage.gifts = res.totalPage;
          this.setData({
            gifts_list: list.concat(res.list),
          });
        }
        loading.gifts = false;
        wx.hideLoading();        
      })
      .catch((err) => {
        loading.gifts = false;
        wx.hideLoading();        
          
      })
      // .finally(() => {
      //   loading.gifts = false;
      //   wx.hideLoading();
      // });
  },

  // 新增邀请收益
  initMyProfitInviter() {
    // if(page> this.data.totalPage.fans){
    //   
    //   return ;
    // }
    if (loading.invites) {
      return;
    }
    wx.showLoading({
      title: "正在加载中",
      icon: "none",
    });

    loading.invites = true;
    getMyProfitInviter({
      showCount: showCount.invites,
      currentPage: currentPage.invites,
      chooseTime: this.data.chooseTime,
    })
      .then((res) => {
        this.handleMoneyInfo(res.moneyInfo);
        if (!res.list) {
          this.setData({
            invites_list: [],
          });
        } else {
          const list = this.data.invites_list;
          for (let i = 0; i < res.list.length; i++) {
            if (res.list[i].phone) {
              res.list[i].private_phone = `${res.list[i].phone.slice(
                0,
                3
              )}****${res.list[i].phone.slice(7, 11)}`;
            }
          }
          ;
          currentPage.invites++;
 
          totalPage.invites = res.totalPage;          
          this.setData({
            invites_list: list.concat(res.list),
          });
        }
        loading.invites = false;

        wx.hideLoading();        
      })
      .catch((err) => {
          
        loading.invites = false;

        wx.hideLoading();        
      })
      // .finally(() => {
      //   loading.invites = false;

      //   wx.hideLoading();
      // });
  },

  // 新增游戏收益
  initMyProfitGame() {
    if (loading.games) {
      return;
    }
    wx.showLoading({
      title: "正在加载中",
      icon: "none",
    });

    loading.games = true;
    getMyProfitGame({
      // showCount: showCount.games,
      // currentPage: currentPage.games,
      chooseTime: this.data.chooseTime,
    })
      .then((res) => {
         
        this.handleMoneyInfo(res.moneyInfo);
        loading.games = false;
        wx.hideLoading();
      })
      .catch((err) => {
        loading.games = false;
        wx.hideLoading();        
          
      })
      // .finally(() => {
      //   loading.games = false;
      //   wx.hideLoading();
      // });
  },

  //根据 moneyInfo 处理tabs数据
  handleMoneyInfo(moneyInfo) {
    this.setData({
      moneyInfo: moneyInfo,
    });
    if (moneyInfo) {
      ;
      this.setData({
        [_tabNumGift]: moneyInfo.earning_gift || 0, 
        [_tabNumFan]: moneyInfo.personInfo
          ? moneyInfo.personInfo.pcu || 0
          : "0",
        [_tabNumGame]: moneyInfo.gprofit || 0,

        [_tabNumInvite]: moneyInfo.share_income || 0,
      });
    } else {
      this.setData({
        [_tabNumGift]: 0,
        [_tabNumFan]: 0,
        [_tabNumGame]: 0,
        [_tabNumInvite]: 0,

        fans_list: [],
        gifts_list: [],
        invites_list: [],
      });
    }
  },
});
